from django.db import models
from db.base_model import BaseModel


class GoodsType(BaseModel):
    '''商品分类模型'''
    name = models.CharField(max_length=20, verbose_name='种类名称')
    index = models.SmallIntegerField(default=0, verbose_name='显示顺序')

    class Meta:
        db_table = 'df_goods_type'
        verbose_name = '商品种类'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Brand(BaseModel):
    name = models.CharField(max_length=20, verbose_name='商品品牌')

    class Meta:
        db_table = 'df_good_brand'
        verbose_name = '商品品牌'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class Goods(BaseModel):
    '''商品模型类'''

    status_choice = (
        (0, '下线'),
        (1, '上线')
    )
    type = models.ForeignKey('GoodsType', verbose_name='商品种类', on_delete=models.CASCADE)
    brand = models.ForeignKey('Brand', verbose_name='商品品牌', on_delete=models.CASCADE)
    name = models.CharField(max_length=20, verbose_name='商品名称')
    desc = models.CharField(max_length=256, verbose_name='商品简介')
    price = models.DecimalField(max_digits=10, decimal_places=2, verbose_name='商品价格')
    unite = models.CharField(max_length=5, verbose_name='商品单位')
    image = models.ImageField(upload_to='goods', verbose_name='商品图片')
    status = models.SmallIntegerField(default=1, choices=status_choice, verbose_name='商品状态')
    stock = models.IntegerField(default=0, verbose_name='库存')

    class Meta:
        db_table = 'db_goods'
        verbose_name = '商品'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class GoodsBanner(BaseModel):
    goods = models.ForeignKey('Goods', verbose_name='商品名', on_delete=models.CASCADE)
    image = models.ImageField(upload_to='banner', verbose_name='图片')
    index = models.SmallIntegerField(default=0, verbose_name='展示顺序')

    class Meta:
        db_table = 'df_banner'
        verbose_name = '轮播'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.goods.name + '轮播图'


class IndexPromote(BaseModel):
    '''促销'''
    name = models.CharField(max_length=20, verbose_name='商品名称')
    image = models.ImageField(upload_to='banner', verbose_name='活动图片')
    index = models.IntegerField(default=0, verbose_name='显示顺序')

    class Meta:
        db_table = 'df_promote'
        verbose_name = '促销'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name
